<template>
  <div class="container">
    <router-link to="/weather" class="title">
      <FontAwesomeIcon :icon="faSun" />
      新中地天气</router-link
    >
    <div class="weather-info">
      <h4>{{ weatherStore.currentCity }}</h4>
      <span>
        实时天气 : {{ weatherStore.currentWeather.weather }}
        {{ weatherStore.currentWeather.temperature }}℃
      </span>
      <span>
        {{ weatherStore.currentWeather.winddirection }}风{{
          weatherStore.currentWeather.windpower
        }}级
      </span>
    </div>
    <div class="about">
      <FontAwesomeIcon :icon="faCircleInfo" @click="closeAboutPopup = true" />
      <FontAwesomeIcon :icon="faPlus" @click="addCity" v-show="IsShow" />
    </div>
  </div>
  <!-- 使用Transition组件包裹弹窗 -->
  <Transition name="fade">
    <div
      v-if="closeAboutPopup"
      class="about-popup-overlay"
      @click.self="closeAboutPopup = false"
    >
      <Transition name="slide-up">
        <div v-if="closeAboutPopup" class="about-popup-content">
          <div class="about-popup-body">
            <h2>关于:</h2>
            <p>这个应用可以用来追踪你选择城市的当前天气</p>
            <h2>如何使用:</h2>
            <p>
              1.点击搜索框输入你希望追踪的城市<br />
              2.在搜索结果中选中一个城市，你将获取当地最新的天气<br />
              3.点击右侧的＋号可以将追踪城市的天气情况保存在首页
            </p>
            <h2>移除城市:</h2>
            <p>如果你不想在首页关注某个城市,可以通过底部的按钮删<br />除它</p>
          </div>
          <div class="about-popup-footer">
            <button @click="closeAboutPopup = false">关闭</button>
          </div>
        </div>
      </Transition>
    </div>
  </Transition>
</template>

<script setup>
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faSun, faCircleInfo, faPlus } from '@fortawesome/free-solid-svg-icons'
import { onMounted, ref, computed } from 'vue'
import { useWeatherStore } from '@/stores/WeatherStore'
import { useRoute } from 'vue-router'
import { getWeather } from '@/api/weather'
const route = useRoute()
const weatherStore = useWeatherStore()
const cityObj = ref({})

// 控制关于弹窗的显示和隐藏
const closeAboutPopup = ref(false)
onMounted(async () => {
  await weatherStore.getCurrentCityWeather()
})

// 从路由中获取city,adcode,请求weather
const addCity = async () => {
  try {
    // 异步获取天气数据
    const weatherData = await getWeather(route.params.key, 'base')
    // 创建城市对象
    cityObj.value = {
      city: route.query.search,
      adcode: route.params.key,
      weather: weatherData.lives[0],
    }
    // 添加到store
    weatherStore.addCity(cityObj.value)
    console.log('城市添加成功:', cityObj.value)
  } catch (error) {
    console.error('添加城市失败:', error)
  }
}
// 判断是否显示添加按钮
const IsShow = computed(() => {
  return (
    route.path !== '/weather' &&
    !weatherStore.cityList.some((city) => city.adcode === route.params.key)
  )
})
</script>

<style scoped lang="scss">
@import 'http://at.alicdn.com/t/c/font_4870270_86cyk0tsx98.css';
$box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
  0 4px 6px -4px rgba(0, 0, 0, 0.1);
.container {
  display: flex;
  box-shadow: $box-shadow;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  background-color: #00668a;
  padding: 0.5rem 1rem;
  .title {
    font-size: 1.5rem;
    line-height: 2rem;
    text-decoration: none;
    color: #fff;
    margin-right: 1rem;
    .fa-sun {
      font-size: 1.5rem;
    }
  }
  .weather-info {
    display: flex;
    align-items: center;
    gap: 1rem;
    h4 {
      font-size: 1.125rem;
      margin: 0;
    }
    span {
      font-size: 0.875rem;
    }
  }
  .about {
    display: flex;
    flex: 1 1 0;
    justify-content: flex-end;
    .fa-circle-info,
    .fa-plus {
      font-size: 20px;
      cursor: pointer;
      padding: 0.25rem 0.5rem;
      &:hover {
        opacity: 0.5;
      }
    }
    .add {
      background-color: #bedc19;
      border-radius: 4px;
      margin-left: 0.5rem;
    }
  }
}

/* 弹窗样式 */
.about-popup-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
.about-popup-content {
  background-color: white;
  border-radius: 8px;
  min-width: 320px;
  max-width: 768px;
  color: #333;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}
.about-popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background-color: #00668a;
  color: white;
  h3 {
    margin: 0;
    font-size: 1.2rem;
  }
  .close-btn {
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    &:hover {
      opacity: 0.8;
    }
  }
}
.about-popup-body {
  padding: 1rem;
  h2 {
    font-size: 16px;
    margin-top: 0.8rem;
  }
  p {
    font-size: 12px;
    margin: 0.5rem 0;
  }
}
.about-popup-footer {
  padding: 1rem;
  text-align: left;
  button {
    background-color: #00668a;
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    &:hover {
      background-color: darken(#00668a, 5%);
    }
  }
}
//给弹窗添加渐入和渐出动画
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
//给弹窗添加滑动动画
.slide-up-enter-active,
.slide-up-leave-active {
  transition: transform 0.3s ease, opacity 0.3s ease;
}
.slide-up-enter-from,
.slide-up-leave-to {
  transform: translateY(30px);
  opacity: 0;
}
</style>
